tmp = 4000;
function start() {
	$.ajax({	
		url: "getState.php",
		type: "POST",
		dataType : "json",
		success: function(data) {
			/* `data` contains parsed JSON */
			if(data["etat"] == 0){
				setTimeout("start()",tmp);
				$("#wait").html("<h1><center>Wait "+data['description']+" <br /> the competition will start soon ...<br /><img src=./images/uploads/affiche_A3.jpg height=500px ></center></h1>");
				$("table").hide();
			}
			else{
				$("#wait").hide();
				$("table").show();
				NextQuestion();
			}
	 }
	});	
}
/*
$("#next_button").click(function() {
  alert("Handler for .click() called.");
});
*/
function NextQuestion(){
/* get the choice of the user from the input tags and put it into the response variable */

if($('input[name=choix]:radio:checked').val()){
var reponse_user = $('input[name=choix]:radio:checked').val();
}
/* if we are in the first run of the script and the client select send the
 * response of no question 
 */
else{ 
var reponse_user = "nothing_set";
}
/* send the current choice and get the new Question/Responses from the server 
 * and put the result into a jqXHR variable 
 * */ 
$.ajax({	
	url: "NextQuestion.php",
	data: {reponse_id : reponse_user}, //send the previous question
	type: "POST",
	dataType : "json",
	statusCode: {
   		404: function() {alert("page not found");}
	},
	success: function(data) {
		/* `data` contains parsed JSON */
		data = data[0];
		var i=1;
		for(var j=1;j<=10;j++){
			$("#choix"+j).hide(); 
			$("#pro"+j).hide();
		}
		$.each(data, function(key, val) {	
				if(key == "question"){
					$("#question").text(data.question);
					if(data.question == null){
						/*if we are in the end of QCM, we clear the table and we fill it with the 
						 end of competition message*/
						$("table").html("");
						$("#score").html("<h2>The competition is end <br /> Your score is </h2><h1>"+data.score+"</h1><h2>pts</h2>");
						// send smothing to tell the server that w're done the test
					}
				}
				else if(key == "score"){
					//$("#score").text(data.score);
				}
				else if(key == "image_path"){
					$("#image_path").html("<img src=./images/uploads/"+data.image_path+">");
				}
				else{
					//alert(key);
					$("#choix"+i).attr("value",key);
					$("#choix"+i).show();
					$("#pro"+i).text(val);
					$("#pro"+i).show();
					i++;
				}
			/* desactivate this for debuging */
    		//alert(i+" => "+key+"=>"+val);
  		});
 }
});

/* parse the data from the jqXHR Object to a javascript variables */

///jqXHR for ...
/* select the div table and fill it with the new Qustion/Responses */
//$('table'). fade ...
}
